<template>
  <el-dialog :title="title" :visible="isShow" width="630px" @close="closeFn">
    <el-form ref="addForm" :rules="rules" :model="addForm">
      <el-form-item prop="regionName" label="区域名称：" label-width="150px">
        <el-input
          style="width: 85%"
          v-model="addForm.regionName"
          placeholder="请输入"
          maxlength="15"
          show-word-limit
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="备注说明:" label-width="150px" prop="remark">
        <el-input
          style="width: 85%"
          type="textarea"
          :rows="4"
          placeholder="请输入备注（不超过40字）"
          maxlength="40"
          show-word-limit
          v-model="addForm.remark"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-col :span="8">
          <el-button
            class="btn cancel"
            @click="closeFn"
            style="margin-right: 20px"
            >取消</el-button
          >
          <el-button class="btn confirm" type="primary" @click="submit"
            >确认</el-button
          >
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { addRegionApi, editRegionApi } from "@/api/node";
export default {
  props: {
    isShow: Boolean,
    title: String,
    regionNow: Object,
  },
  data() {
    return {
      addForm: {
        regionName: "",
        remark: "",
      },
      rules: {
        regionName: [
          { required: true, message: "请输入区域名称", trigger: "blur" },
        ],
        remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getData()
  },
  methods: {
    closeFn() {
      this.$refs.addForm.resetFields();
      this.addForm = { regionName: "", remark: "" };
      this.$emit("update:isShow", false);
    },
    getData() {
      if (this.title === "修改区域") {
        this.addForm = {
        regionName: this.regionNow.regionName,
        remark: this.regionNow.remark,
      }
      }
    },
    submit() {
      this.$refs.addForm.validate(async (pass) => {
        if (pass) {
          if (this.title === "新增区域") {
            await addRegionApi(this.addForm);
            this.$message.success("添加区域成功");
            this.closeFn();
            this.$emit('searchRegion')
          } else {
            console.log(this.addForm);
            await editRegionApi(this.regionNow.id,this.addForm);
            this.$message.success("修改成功");
            this.closeFn();
            this.$emit('searchRegion')
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.btn {
  width: 80px;
  height: 36px;
  border: none;
  padding: 0;
  line-height: 36px;
}
.cancel {
  background-color: #fbf4f0;
  color: #7e7570;
}
.confirm {
  background-color: #ff6d29;
  color: #fff;
}
</style>
